/*
 * @Author: C.
 * @Date: 2021-12-24 16:01:43
 * @LastEditTime: 2021-12-24 16:04:12
 * @Description: file content
 */
//最近最少使用
//设置容量 超出容量=》 新键顶替旧键
// 每次操作重新删除再新增当前key 以更新索引 超出尺寸则删除旧索引
class LRUCache {
  constructor(size=7) {
    this.size = size
    this.cache = new Map()
  }

  get(key) {
    const hasKey = this.cache.has(key)
    if (hasKey) {
      const val = this.cache.get(key)
      this.cache.delete(key)
      this.cache.set(key, val)
      return val
    } else {
      return -1
    }
  }

  put(key, val) {
    const hasKey = this.cache.has(key)
    if (hasKey) {
      this.cache.delete(key)
    }
    this.cache.set(key, val)
    if (this.cache.size > this.size) {
      this.cache.delete(this.cache.keys().next().value)
    }
  }

}